package com.gamificationlibrary.view;

import com.gamificationlibrary.adapter.LeaderboardAdapter;
import com.gamificationlibrary.gamificationlibrary.R;
import com.gamificationlibrary.model.Player;
import com.gamificationlibrary.model.leaderboard.Leaderboard;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import android.widget.ListView;
import android.widget.TextView;



public class LeaderboardView extends ListView{

	private LeaderboardAdapter adapter;
	private View headerView;
	private View footerView;
	private int numberRowsVisible=0;
	private TextView footerRank;
	private TextView footerUsername;
	private TextView footerScore;
	public static final String EXTRAS_LEADERBOARD = "EXTRAS_LEADERBOARD";
	public static final String EXTRAS_PLAYER = "EXTRAS_PLAYER";
	 
	public LeaderboardView(Context context) {
		
		super(context);
		
		addHeader();
		addFooter();
	}	

	 //This example uses this method since being built from XML
	 public LeaderboardView(Context context, AttributeSet attrs)
	 {
	    super(context, attrs);
	  
		addHeader();
		addFooter();
		
	 }
	  //Build from XML layout
	 public LeaderboardView(Context context, AttributeSet attrs, int defStyle)
	 {
	    super(context, attrs, defStyle);
	    
	  		addHeader();
	  		addFooter();	    
		
	 }
	  
	 public void putContent(Leaderboard l,String currentIdentification)
	 {
		Player pCurrent=l.getPlayerfromIdentification(currentIdentification);
		setTextFooter(String.valueOf(l.getRankPlayer(pCurrent)), currentIdentification, String.valueOf(l.getScorePlayer(pCurrent).getValue()));
		setTypeFaceFooter(Typeface.BOLD);
		adapter = new LeaderboardAdapter(getContext(), setNumberRowVisibles(l), pCurrent);
		setAdapter(adapter); 
		
	 }
	 
	 public void addHeader()
	 {
		 headerView = View.inflate(getContext(), R.layout.leaderboard_header, null);
		 addHeaderView(headerView);
	 }
	 
	 public void addFooter()
	 {
		 footerView = View.inflate(getContext(), R.layout.leaderboard_header, null);
		 
		 footerRank = (TextView) footerView.findViewById(R.id.text_rank); 
		 footerUsername = (TextView) footerView.findViewById(R.id.text_username); 
		 footerScore = (TextView) footerView.findViewById(R.id.text_score);
			
		 addFooterView(footerView);
	 }
	 
	 public void removeHeader()
	 {
		 removeHeaderView(headerView);
	 }
	 
	 public void removeFooter()
	 {
		 removeFooterView(footerView);
	 }
	 
	 public void setTextFooter(String rank, String username, String score)
	 {
			footerRank.setText(rank);
			footerUsername.setText(username);
			footerScore.setText(score);
	 }
	 
	 public void setTypeFaceFooter(int typeFace)
	 {
			footerRank.setTypeface(null, typeFace);
		   	footerUsername.setTypeface(null, typeFace);
		   	footerScore.setTypeface(null, typeFace);
	 }
	 
	 public void setNumberRowVisibles(int number)
	 {
		 this.numberRowsVisible=number;
	 }
	 
	 private Leaderboard setNumberRowVisibles(Leaderboard l)
	 {
		 Leaderboard newL = null;
		 newL=l;
		 l.setNumberPlayersVisibles(numberRowsVisible);		
		 return newL;
	 }
	


	
}
